
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   
      <title>3.3.&nbsp;Tuple 介绍</title>
      <link rel="stylesheet" href="../diveintopython.css" type="text/css">
      <link rev="made" href="mailto:f8dy@diveintopython.org">
      <meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
      <meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
      <meta name="description" content="Python from novice to pro">
      <link rel="home" href="../toc/index.html" title="Dive Into Python">
      <link rel="up" href="index.html" title="第&nbsp;3&nbsp;章&nbsp;内置数据类型">
      <link rel="previous" href="lists.html" title="3.2.&nbsp;List 介绍">
      <link rel="next" href="declaring_variables.html" title="3.4.&nbsp;变量声明">
   </head>
   <body>
      <table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td id="breadcrumb" colspan="5" align="left" valign="top">导航：<a href="../index.html">起始页</a>&nbsp;&gt;&nbsp;<a href="../toc/index.html">Dive Into Python</a>&nbsp;&gt;&nbsp;<a href="index.html">内置数据类型</a>&nbsp;&gt;&nbsp;<span class="thispage">Tuple 介绍</span></td>
            <td id="navigation" align="right" valign="top">&nbsp;&nbsp;&nbsp;<a href="lists.html" title="上一页: “List 介绍”">&lt;&lt;</a>&nbsp;&nbsp;&nbsp;<a href="declaring_variables.html" title="下一页: “变量声明”">&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3" id="logocontainer">
               <h1 id="logo"><a href="../index.html" accesskey="1">深入 Python :Dive Into Python 中文版</a></h1>
               <p id="tagline">Python 从新手到专家 [Dip_5.4b_CPyUG_Release]</p>
            </td>
            <td colspan="3" align="right">
               <form id="search" method="GET" action="http://www.google.com/custom">
                  <p><label for="q" accesskey="4">Find:&nbsp;</label><input type="text" id="q" name="q" size="20" maxlength="255" value=""> <input type="submit" value="搜索"><input type="hidden" name="domains" value="woodpecker.org.cn/diveintopython"><input type="hidden" name="sitesearch" value="www.woodpecker.org.cn/diveintopython"></p>
               </form>
            </td>
         </tr>
      </table>
      <!--#include virtual="/inc/ads" -->
      <div class="section" lang="zh_cn">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title"><a name="odbchelper.tuple"></a>3.3.&nbsp;Tuple 介绍
                  </h2>
               </div>
            </div>
            <div></div>
         </div>
         <div class="abstract">
            <p>Tuple 是不可变的 list。一旦创建了一个 tuple，就不能以任何方式改变它。</p>
         </div>
         <div class="example"><a name="d0e6712"></a><h3 class="title">例&nbsp;3.15.&nbsp;定义 tuple</h3><pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">t = (<span class='pystring'>"a"</span>, <span class='pystring'>"b"</span>, <span class='pystring'>"mpilgrim"</span>, <span class='pystring'>"z"</span>, <span class='pystring'>"example"</span>)</span> <a name="odbchelper.tuple.1.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">t</span>
<span class="computeroutput">('a', 'b', 'mpilgrim', 'z', 'example')</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">t[0]</span>                                       <a name="odbchelper.tuple.1.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput">'a'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">t[-1]</span>                                      <a name="odbchelper.tuple.1.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<span class="computeroutput">'example'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">t[1:3]</span>                                     <a name="odbchelper.tuple.1.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput">('b', 'mpilgrim')</span></pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#odbchelper.tuple.1.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">定义 tuple 与定义 list 的方式相同，但整个元素集是用小括号包围的，而不是方括号。</td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#odbchelper.tuple.1.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">Tuple 的元素与 list 一样按定义的次序进行排序。Tuples 的索引与 list 一样从 0 开始，所以一个非空 tuple 的第一个元素总是 <tt class="literal">t[0]</tt>。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#odbchelper.tuple.1.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">负数索引与 list 一样从 tuple 的尾部开始计数。</td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#odbchelper.tuple.1.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">与 list 一样分片 (slice) 也可以使用。注意当分割一个 list 时，会得到一个新的 list ；当分割一个 tuple 时，会得到一个新的 tuple。</td>
                  </tr>
               </table>
            </div>
         </div>
         <div class="example"><a name="odbchelper.tuplemethods"></a><h3 class="title">例&nbsp;3.16.&nbsp;Tuple 没有方法</h3><pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">t</span>
<span class="computeroutput">('a', 'b', 'mpilgrim', 'z', 'example')</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">t.append(<span class='pystring'>"new"</span>)</span>    <a name="odbchelper.tuple.2.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="traceback">Traceback (innermost last):
  File "&lt;interactive input&gt;", line 1, in ?
AttributeError: 'tuple' object has no attribute 'append'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">t.remove(<span class='pystring'>"z"</span>)</span>      <a name="odbchelper.tuple.2.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="traceback">Traceback (innermost last):
  File "&lt;interactive input&gt;", line 1, in ?
AttributeError: 'tuple' object has no attribute 'remove'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">t.index(<span class='pystring'>"example"</span>)</span> <a name="odbchelper.tuple.2.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<span class="traceback">Traceback (innermost last):
  File "&lt;interactive input&gt;", line 1, in ?
AttributeError: 'tuple' object has no attribute 'index'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pystring'>"z"</span> <span class='pykeyword'>in</span> t</span>           <a name="odbchelper.tuple.2.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput">True</span></pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#odbchelper.tuple.2.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">您不能向 tuple 增加元素。Tuple 没有 <tt class="function">append</tt> 或 <tt class="function">extend</tt> 方法。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#odbchelper.tuple.2.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">您不能从 tuple 删除元素。Tuple 没有 <tt class="function">remove</tt> 或 <tt class="function">pop</tt> 方法。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#odbchelper.tuple.2.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">您不能在 tuple 中查找元素。Tuple 没有 <tt class="function">index</tt> 方法。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#odbchelper.tuple.2.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">然而，您可以使用 <tt class="function">in</tt> 来查看一个元素是否存在于 tuple 中。
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <p>那么使用 tuple 有什么好处呢？</p>
         <div class="itemizedlist">
            <ul>
               <li>Tuple 比 list 操作速度快。如果您定义了一个值的常量集，并且唯一要用它做的是不断地遍历它，请使用 tuple 代替 list。</li>
               <li>如果对不需要修改的数据进行 “<span class="quote">写保护</span>”，可以使代码更安全。使用 tuple 而不是 list 如同拥有一个隐含的 <tt class="literal">assert</tt> 语句，说明这一数据是常量。如果必须要改变这些值，则需要执行 tuple 到 list 的转换 (需要使用一个特殊的函数)。
               </li>
               <li>还记得我说过 <a href="index.html#odbchelper.dictionarytypes" title="例&nbsp;3.4.&nbsp;在 dictionary 中混用数据类型">dictionary keys</a> 可以是字符串，整数和 “<span class="quote">其它几种类型</span>”吗？Tuples 就是这些类型之一。Tuples 可以在 dictionary 中被用做 key，但是 list 不行。实际上，事情要比这更复杂。Dictionary key 必须是不可变的。Tuple 本身是不可改变的，但是如果您有一个 list
                  的 tuple，那就认为是可变的了，用做 dictionary key 就是不安全的。只有字符串、整数或其它对 dictionary 安全的 tuple 才可以用作 dictionary key。
               </li>
               <li>Tuples 可以用在字符串格式化中，我们会很快看到。</li>
            </ul>
         </div><a name="tip.tuple"></a><table class="note" border="0" summary="">
            <tr>
               <td rowspan="2" align="center" valign="top" width="1%"><img src="../images/note.png" alt="注意" title="" width="24" height="24"></td>
            </tr>
            <tr>
               <td colspan="2" align="left" valign="top" width="99%">Tuple 可以转换成 list，反之亦然。内置的 <tt class="function">tuple</tt> 函数接收一个 list，并返回一个有着相同元素的 tuple。而 <tt class="function">list</tt> 函数接收一个 tuple 返回一个 list。从效果上看，<tt class="function">tuple</tt> 冻结一个 list，而 <tt class="function">list</tt> 解冻一个 tuple。
               </td>
            </tr>
         </table>
         <div class="furtherreading">
            <h3>进一步阅读</h3>
            <ul>
               <li><a href="http://www.ibiblio.org/obp/thinkCSpy/" title="Python book for computer science majors"><i class="citetitle">How to Think Like a Computer Scientist</i></a> 讲解了 tuple 并且展示了如何<a href="http://www.ibiblio.org/obp/thinkCSpy/chap10.htm">连接 tuple</a>。
               </li>
               <li><a href="http://www.faqts.com/knowledge-base/index.phtml/fid/199/"><span class="application">Python</span> Knowledge Base</a> 展示了如何对<a href="http://www.faqts.com/knowledge-base/view.phtml/aid/4553/fid/587">一个 tuple 排序</a>。
               </li>
               <li><a href="http://www.python.org/doc/current/tut/tut.html"><i class="citetitle"><span class="application">Python</span> Tutorial</i></a> 展示了如何<a href="http://www.python.org/doc/current/tut/node7.html#SECTION007300000000000000000">定义一个只包含一个元素的 tuple</a>。
               </li>
            </ul>
         </div>
      </div>
      <table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td width="35%" align="left"><br><a class="NavigationArrow" href="lists.html">&lt;&lt;&nbsp;List 介绍</a></td>
            <td width="30%" align="center"><br>&nbsp;<span class="divider">|</span>&nbsp;<a href="index.html#odbchelper.dict" title="3.1.&nbsp;Dictionary 介绍">1</a> <span class="divider">|</span> <a href="lists.html" title="3.2.&nbsp;List 介绍">2</a> <span class="divider">|</span> <span class="thispage">3</span> <span class="divider">|</span> <a href="declaring_variables.html" title="3.4.&nbsp;变量声明">4</a> <span class="divider">|</span> <a href="formatting_strings.html" title="3.5.&nbsp;格式化字符串">5</a> <span class="divider">|</span> <a href="mapping_lists.html" title="3.6.&nbsp;映射 list">6</a> <span class="divider">|</span> <a href="joining_lists.html" title="3.7.&nbsp;连接 list 与分割字符串">7</a> <span class="divider">|</span> <a href="summary.html" title="3.8.&nbsp;小结">8</a>&nbsp;<span class="divider">|</span>&nbsp;
            </td>
            <td width="35%" align="right"><br><a class="NavigationArrow" href="declaring_variables.html">变量声明&nbsp;&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3"><br></td>
         </tr>
      </table>
      <div class="Footer">
         <p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
         <p class="copyright">Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007 <a href="mailto:python-cn@googlegroups.com">CPyUG (邮件列表)</a></p>
      </div>
   </body>
</html>